package com.gange.service.impl;

import com.gange.entity.Category;
import com.gange.entity.Favorite;
import com.gange.entity.User;
import com.gange.entity.Video;
import com.gange.dao.VideoMapper;
import com.gange.feignclients.CategoryClients;
import com.gange.feignclients.UserClients;
import com.gange.service.IVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gange.utils.JSONUtils;
import com.gange.utils.RedisPrefix;
import com.gange.vo.VideoDetailVO;
import com.jayway.jsonpath.internal.Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 视频 服务实现类
 * </p>
 *
 * @author gange
 * @since 2022-07-23
 */
@Service
@Slf4j
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {
    @Autowired
    private CategoryClients categoryClients;
    @Autowired
    private UserClients userClients;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public VideoDetailVO queryDetailById(String videoId, String token) {
        //1.根据id查询视频信息
        Video video = baseMapper.selectById(Integer.parseInt(videoId));

        //2.创建videoDetailVo
        VideoDetailVO videoDetailVO = new VideoDetailVO();

        //3.将video对象中基本信息复制到VideoDetailVo {id title cover link created_at update_at}
        BeanUtils.copyProperties(video, videoDetailVO);

        //4.设置当前视频类别名称  VideoDetailVo {id title cover link created_at update_at category}
        Category category = categoryClients.category(video.getCategoryId().toString());//调用类别服务
        log.info("根据类别id查询到的类别信息为: {}", JSONUtils.writeValueAsString(category));
        videoDetailVO.setCategory(category.getName());


        //5.设置用户信息 VideoDetailVo {id title cover link created_at update_at category uploader}
        User user = userClients.user(video.getUid().toString());//调用用户服务
        log.info("根据id查询用户信息为: {}", JSONUtils.writeValueAsString(video));
        videoDetailVO.setUploader(user);

        //6.设置播放次数 {id title cover link created_at update_at category uploader plays_count}
        videoDetailVO.setPlaysCount(0);//初始化默认值为0
        Object playedCountsObject = redisTemplate.opsForValue().get(RedisPrefix.VIDEO_PLAYED_COUNT_PREFIX + videoId);

        if (!ObjectUtils.isEmpty(playedCountsObject)) {
            log.info("当前视频视频播放次数为: {}", playedCountsObject.toString());
            videoDetailVO.setPlaysCount(Integer.valueOf(playedCountsObject.toString()));
        }


        //7.设置点赞数 {id title cover link created_at update_at category uploader plays_count likes_count}
        videoDetailVO.setLikesCount(0);//初始化默认值为0
        Object likedCountsObj = redisTemplate.opsForValue().get(RedisPrefix.VIDEO_LIKE_COUNT_PREFIX + video.getId());
        if (!ObjectUtils.isEmpty(likedCountsObj)) {
            log.info("当前视频点赞数量为: {}", likedCountsObj);
            videoDetailVO.setLikesCount(Integer.valueOf(likedCountsObj.toString()));
        }


        //8.设置当前用户是否喜欢  不喜欢  收藏 {id title cover link created_at update_at category uploader plays_count likes_count liked  disabled favorite}
        User o = (User) redisTemplate.opsForValue().get("session_" + token);
        if (!ObjectUtils.isEmpty(o)) {
            //设置是否喜欢
            Boolean liked = redisTemplate.opsForSet().isMember(RedisPrefix.USER_LIKE_PREFIX + o.getId(), videoId);
            videoDetailVO.setLiked(liked);
            //设置是否不喜欢
            Boolean disliked = redisTemplate.opsForSet().isMember(RedisPrefix.USER_DISLIKE_PREFIX + o.getId(), videoId);
            videoDetailVO.setDisliked(disliked);
            //设置是否收藏
            Favorite favorite = userClients.favorite(videoId, o.getId().toString());
            log.info("是否收藏过该视频: {}", !ObjectUtils.isEmpty(favorite));
            if (!ObjectUtils.isEmpty(favorite)) {
                videoDetailVO.setFavorite(true);
            }
        }

        return videoDetailVO;
    }
}
